package com.fandou.airobot.ui.alarm

import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.ActivityUtils
import com.fandou.airobot.database.Alarm
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.interfaces.XPopupCallback

/**
 * @author       Jm
 * @date         2020/6/30 17:07
 * @description 闹铃提醒activity
 */
class RemindActivity : AppCompatActivity() {

    private var mAlarm: Alarm? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mAlarm = intent.getParcelableExtra("alarm")
        if (mAlarm == null) {
            finish()
            return
        }


        XPopup.Builder(this).dismissOnTouchOutside(true).setPopupCallback(object : XPopupCallback {
            override fun onBackPressed(): Boolean {
                if (!ActivityUtils.isActivityExistsInStack(AlarmActivity::class.java)) {
                    AlarmActivity.start(this@RemindActivity)
                }
                return false
            }

            override fun onDismiss() {
                val manager = this@RemindActivity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                manager.cancelAll()
            }

            override fun beforeShow() {
            }

            override fun onCreated() {
            }

            override fun onShow() {
            }
        }).asCustom(
            AlarmRemindPop(
                this,
                "闹钟提醒！",
                mAlarm!!.remark
            )
        ).show() as AlarmRemindPop?
    }


    companion object {

        @JvmStatic
        fun getRemindIntent(context: Context, alarm: Alarm): Intent {
            val fullScreenIntent = Intent(context, RemindActivity::class.java)
            fullScreenIntent.putExtra("alarm", alarm)
            fullScreenIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            fullScreenIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            return fullScreenIntent
        }
    }
}